package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.AlbumDetailAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.AlbumDetail;
import com.example.myapplication.entity.HotAlbums;
import com.example.myapplication.entity.HotSongs;
import com.example.myapplication.tools.BarTransparent;
import com.example.myapplication.tools.SetColor;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AlbumDetailActivity extends AppCompatActivity  {
    private ImageView img_singer;
    private MaterialToolbar toolbar;
    private CollapsingToolbarLayout collapsing;
    private RecyclerView recyclerView1;
    private Context context;
    private AlbumDetailAdapter adapter;
    private List<AlbumDetail.SongsBean>songsBeanList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        BarTransparent.makeStatusBarTransparent(this);
        context=this;
        img_singer = findViewById(R.id.img_singer);
        toolbar = findViewById(R.id.toolbar);
        collapsing = findViewById(R.id.main_collapsing);
        recyclerView1 = findViewById(R.id.recyclerView1);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        HotAlbums hotAlbums = (HotAlbums) bundle.getSerializable("hotAlbums");
        toolbar.setTitle(hotAlbums.getName());
        SetColor.asBitmap(hotAlbums.getBlurPicUrl(),this,collapsing);
        Glide.with(this).load(hotAlbums.getBlurPicUrl())
                .error(R.drawable.ic_baseline_refresh_24)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .centerCrop()
                .into(img_singer);

        RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
        recyclerView1.setLayoutManager(manager);
        adapter = new AlbumDetailAdapter();
        recyclerView1.setAdapter(adapter);
        loadData(hotAlbums.getId());
        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void onclick(int position) {
                AlbumDetail.SongsBean songsBean = songsBeanList.get(position);

                Gson gson = new Gson();
                String json = gson.toJson(songsBean);
                HotSongs hotSongs = gson.fromJson(json,HotSongs.class);

                Intent intent1 = new Intent(context, SongDetail1Activity.class);

                Bundle bundle = new Bundle();
                bundle.putSerializable("song",hotSongs);
                intent1.putExtras(bundle);
                startActivity(intent1);
            }
        });
    }

    private void loadData(int id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<AlbumDetail> call = service.getAlbumDetail(id);
        call.enqueue(new Callback<AlbumDetail>() {
            @Override
            public void onResponse(Call<AlbumDetail> call, Response<AlbumDetail> response) {
                AlbumDetail baseResponse = response.body();
                songsBeanList = baseResponse.getSongs();
                adapter.setData(songsBeanList);

            }

            @Override
            public void onFailure(Call<AlbumDetail> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}